package com.haiyu.manager.controller.platform;

import com.haiyu.manager.dto.BoardSearchDTO;
import com.haiyu.manager.dto.boardStatusDTO;
import com.haiyu.manager.pojo.Board;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.BoardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("board")//指定路由
public class BoardController {
    @Autowired
    private BoardService boardService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     *
     * 功能描述: 跳到board列表
     *
     *
     */
    @RequestMapping("/boardManage")
    public String boardManage() {
        return "/board/boardManage";
    }

    /**
     *
     * 功能描述: 分页查询board列表
     *
     *
     */
    @RequestMapping(value = "/getBoardList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getBoardList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize,/*@Valid PageRequest page,*/ BoardSearchDTO boardSearch) {
        /*logger.info("分页查询用户列表！搜索条件：userSearch：" + userSearch + ",pageNum:" + page.getPageNum()
                + ",每页记录数量pageSize:" + page.getPageSize());*/
       System.out.println(boardSearch);
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = boardService.getBoardList(boardSearch, pageNum ,pageSize);
            logger.info("board列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("board列表查询异常！", e);
        }
        return pdr;
    }



    /**
     *
     * 功能描述: 更新board
     *
     *
     */
    @RequestMapping(value = "/setBoard", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setBoard(Board board) {
        logger.info("设置board[更新]！board:" + board);
        System.out.println("board");
        Map<String,Object> data = new HashMap();

         data = boardService.updateBoard(board);
        return data;
    }


    /**
     *
     * 功能描述: 删除/恢复 board
     *
     *
     */
    @RequestMapping(value = "/updateBoardStatus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateBoardStatus(@RequestParam("boardId") Integer boardId,@RequestParam("boardStatus") Integer boardStatus) {
        logger.info("删除/恢复留言！boardId:" + boardId+" boardStatus:"+boardStatus);
        Map<String, Object> data = new HashMap<>();
        if(boardStatus == 1){
            //删除liuyan
            data = boardService.delBoard(boardId,boardStatus);
        }else{
            //恢复liuyan
            data = boardService.recoverBoard(boardId,boardStatus);
        }
        return data;
    }

    @GetMapping("getBoardStatus")
    @ResponseBody
    public List<boardStatusDTO> getBoardStatus(){
        boardStatusDTO boardStatusDTO1 = new boardStatusDTO();
        boardStatusDTO boardStatusDTO2 = new boardStatusDTO();
        boardStatusDTO1.setBoardStatus(0);
        boardStatusDTO1.setName("未读");
        boardStatusDTO2.setBoardStatus(1);
        boardStatusDTO2.setName("已读");
        List<boardStatusDTO> boardStatusDTOS = new ArrayList<boardStatusDTO>();
        boardStatusDTOS.add(boardStatusDTO1);
        boardStatusDTOS.add(boardStatusDTO2);
        return boardStatusDTOS;
    }

}
